<template>
  <div>
    <el-dialog title="更新进展" :visible.sync="dialogShow" width="85%" :close-on-click-modal="false" :close-on-press-escape="false">
      <form-common :filed-list="evolveFiledList" ref="form"></form-common>
      <div class="flex margin-top-20">
        <span class="font-size-16 color-333333 padding-top-5">相关资料</span>
        <upload-file class="upload-box" buttonType="text" btnName="上传资料" :is-multiple="false" filed-name="file" :max-size="100" accept-file-type=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.pdf" :file-list="fileList" file-url-name="fileUrl" file-txt-name="fileTitle" @handleSuccess="handleSuccess"> </upload-file>
      </div>

      <div slot="footer" class="dialog-footer margin-bottom-40">
        <el-button size="mini" class="f-right el-button-reset" @click="dialogShow = false">取 消</el-button>
        <el-button size="mini" class="f-right margin-right-15" type="primary" @click="saveFn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import formCommon from '@g/components/form/formCommon.vue';
import { evolveFiledList } from '../form';
import uploadFile from '../../../components/upload.vue';
import { apiUpdateEvolve } from '../../../api/workTrace';
import { apiConstant } from '../../../api/common';
import { requestTips } from '../../../assets/util';

export default {
  name: 'addWork',
  props:{
    workKey:{
      type:String,
      default:""
    }
  },
  data() {
    return {
      dialogShow: false,
      evolveFiledList: evolveFiledList,
      fileList: [],
    };
  },
  mounted() {
    this.getConstant()
  },
  methods: {
    async saveFn() {
      let model = this.$refs.form.validateForm();
      if (this.fileList.length == 0) {
        this.$message.warning('请上传相关资料');
        return;
      }
      model.attachmentList = this.fileList;
      model.workId = this.workKey
      model.type = 'progressUp'
      let res = await apiUpdateEvolve(model);
      requestTips(res, async () => {
        this.dialogShow = false;
        this.$emit('refresh');
      });
    },

    //常量
    async getConstant() {
      let res = await apiConstant();
      evolveFiledList.forEach(item => {
        if (item.bindFieldName == 'workStatus') {
          item.optionList = res.workProgressStatus;
        }
      });
    },

    //上传成功
    handleSuccess(file, fileList) {
      if (file.data.rows && file.data.rows.length > 0) {
        this.fileList.push({
          fileId: file.data.rows[0].key,
          fileUrl: file.data.rows[0].url,
          fileTitle: file.data.rows[0].originalName,
        });
      } else {
        this.fileList = [];
        this.$message.error(file.data.message);
      }
    },

    init() {
      this.dialogShow = true;
      this.fileList = []
      this.$nextTick(() => {
        this.$refs.form.initModel();
      });
    },
  },
  components: { formCommon, uploadFile },
};
</script>

<style lang="scss" scoped>
.upload-box{
    margin-left: 44px;
}
</style>
